﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using log4net;
using System.Xml;
using System.Xml.XPath;

namespace ConfigUtilLib
{
    public class ProcessorRecord
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private const string nameStr = "name";
        private const string dirStr = "dir";
        //private const string assemblyclassnameStr = "assemblyclassname";
        private const string processorConfigurationFileStr = "processorConfigurationFile";
        private const string assemblyDLLStr = "assemblyDLL";

        public string ProcessorName { get; private set; }
        public string ProcessorDir { get; private set; }
        public string AssemblyDllName { get; private set; }
        public string ProcessConfigFile { get; private set; }


        public bool LoadConfiguration(XmlNode processorCfgNode)
        {

            Console.Write("Reading Processor Data ");
            //1.  Get The name of the processor and dir 
            XmlAttribute xmlAttr = processorCfgNode.Attributes[nameStr];

            if (xmlAttr == null)
            {
                return false;
            }

            ProcessorName = xmlAttr.Value;
            log.DebugFormat("Processor Name: "+ProcessorName);

            xmlAttr = processorCfgNode.Attributes[dirStr];

            if (xmlAttr == null)
            {
                return false;
            }
            ProcessorDir = xmlAttr.Value;

            log.DebugFormat("Processor DIR  :{0}", ProcessorDir);

            // Get DLL Name now 
            XmlNode assemblyDLLNode = processorCfgNode.SelectSingleNode(assemblyDLLStr);
            if (assemblyDLLNode == null)
            {
                return false;
            }

            AssemblyDllName = assemblyDLLNode.InnerText;

            log.DebugFormat("Processor Assemebly DLL Name : " + AssemblyDllName);



            //3. get Assembly ConfigurationFile 

            XmlNode processorConfigFileNameNode = processorCfgNode.SelectSingleNode(processorConfigurationFileStr);
            if (processorConfigFileNameNode != null)
            {
                ProcessConfigFile = processorConfigFileNameNode.InnerText; 
            }
            log.DebugFormat("Processor Configuration File  Name : " + ProcessConfigFile);


            log.DebugFormat("Completed Reading Processor data for :" + ProcessorName);
            return true;
        }
    }
}
